﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using Awesomium.Core;
using Awesomium.ComponentModel;
using System.Threading;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace Diplomova_prace
{
    public partial class Linear_solutions_win : Form
    {
        Linear_Solutions_alg linear = new Linear_Solutions_alg();
        LinearniRovnice_ret ret = new LinearniRovnice_ret();
        Reg reg = new Reg();
        Expression expression = new Expression();
        Methods method;
        string NameMethod;
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        string pathToHtml = string.Empty;
        readonly string temp = string.Empty;
        StreamWriter sw;
        static int instanceCounter;
        readonly int instanceId;
        string formula = string.Empty;
        string result = string.Empty;
        int defaultFontSize = 24;
        Exports export = new Exports();
        List<Control> alignedComponents = new List<Control>();
        bool incorrect = false;
        SaveDialogs save = new SaveDialogs();
        string PathToLastExportFile = string.Empty;
        int offset = 100;
        public enum Methods
        {
            GaussElimination = 0,
            LUDecomposition = 1,
            CholeskyDecomposotion = 2,
            JacobiMethodFolder = 3,
            JacobiMethodMatrix = 4,
            GaussSeidelMethodFolder = 5,
            GaussSeidelMethodMatrix = 6,
            SuperRelaxationMethodFolder = 7,
            SuperRelaxationMethodMatrix = 8
        }
        public Linear_solutions_win(Methods method, string[,] soustava = null, string[,] vektorB = null, string[,] PocatecniAproximace = null, string faktor = "", string pocetIteraci = "", string presnost= "")
        {

            this.method = method;
            this.instanceId = instanceCounter++;
            temp = MainMenu.tempProperty + "LinearSolution";
           
            InitializeComponent();
            DisableComponents();
            AlignComponents();
            expression.incorrectInput += expression_incorrectInput;
            this.FillListChangeMethod(ChangeMethod_ListBox, MainMenu.dictLinTagName);
            NameWindow();
            this.Show();
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
            this.pocetPromennych_numericUpDown.Value = 1;
            this.pocetPromennych_numericUpDown.Minimum = 1;
            AddTask_Button.Enabled = false;
            VypocitejButton.Enabled = false;

            if (soustava != null)
            {
                InsertDataToDataGridView(soustava, Soustava_DatagridView);
                this.pocetPromennych_numericUpDown.Value = soustava.GetLength(1);
            }
            if (vektorB != null)
            {
                InsertDataToDataGridView(vektorB, VektorB_dataGridView);
            }
            if (PocatecniAproximace != null)
            {
                InsertDataToDataGridView(PocatecniAproximace,pocatecni_aproximace_dataGridView);
            }
            if (faktor != "")
            {
                faktor_textBox.Text = faktor;
            }
            if (pocetIteraci != "")
            {
                Pocet_iteraci_textBox.Text = pocetIteraci;
            }
            if (presnost != "")
            {
                Presnost_textBox.Text = presnost;
            }

        }

        void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            VypocitejButton.Enabled = true;
            FontSize_trackBar.Value = defaultFontSize;
            DecimalPlaces_numericUpDown.Enabled = true;
            if (!incorrect)
                webControl1.Source = new Uri(Path.GetFullPath(pathToHtml));
        }

        void expression_incorrectInput()
        {
            if (!incorrect)
            {
                MessageBox.Show("Nekorektní vstup", "chyba", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                sw.Close();
                incorrect = true;
                VypocitejButton.Enabled = true;
            }
        }

        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            double[,] soustava = GetDoubleDataOfDataGridView(Soustava_DatagridView);
            double[,] vektorB = GetDoubleDataOfDataGridView(VektorB_dataGridView);
            double[,] pocatecni_aproximace = null;
            string a = Pocet_iteraci_textBox.Text;
            int pocetIter = -1;
            string b = faktor_textBox.Text;
            string c = Presnost_textBox.Text;
            double presnost = double.NaN;
            double faktor = double.MinValue;
            if ((int)method > 2)
            {
                pocatecni_aproximace = GetDoubleDataOfDataGridView(pocatecni_aproximace_dataGridView);
                pocetIter = (int)expression.ComputeRPN(expression.RPN(a));
                presnost = expression.ComputeRPN(expression.RPN(c));
            }
            if (method == Methods.GaussElimination)
            {

                Tuple<List<double[,]>, List<double[,]>,List<double>, double[,]> ret = linear.GaussElimination(soustava, vektorB);
                result = VypisGaussovaEliminace(ret);
                
            }
            if (method == Methods.LUDecomposition)
            {
                ret = linear.LUDecomposition(soustava, vektorB);
                result = VypisLURozklad(ret);
            }
            else if (method == Methods.CholeskyDecomposotion)
            {
                ret = linear.CholeskyDecomposition(soustava, vektorB);
                result = VypisCholeskehoRozklad(ret);
            }
            else if (method == Methods.JacobiMethodMatrix)
            {
                Tuple<List<double[,]>, List<double[,]>,List<double>> ret = linear.JacobihoMethodMatrix(soustava, vektorB, pocatecni_aproximace, pocetIter, presnost);
                result = VypisJacobihoMetodaMatice(ret);
            }
            else if (method == Methods.JacobiMethodFolder)
            {
                LinearniRovniceSlozky_ret ret = linear.JacobihoMethodFolder(soustava, vektorB, pocatecni_aproximace, pocetIter, presnost);
                result = VypisJacobihoMetodaSlozka(ret);
            }
            else if (method == Methods.GaussSeidelMethodMatrix)
            {
                Tuple<List<double[,]>, List<double[,]>,List<double>> ret = linear.GaussSeidelMethodMatrix(soustava, vektorB, pocatecni_aproximace, pocetIter, presnost);
                result = VypisGaussSeidelMatice(ret);
            }
            else if (method == Methods.GaussSeidelMethodFolder)
            {
                LinearniRovniceSlozky_ret ret = linear.GaussSeidelMethodFolder(soustava, vektorB, pocatecni_aproximace, pocetIter, presnost);
                result = VypisGaussSeidelovaMetodaSlozka(ret);
            }
            else if (method == Methods.SuperRelaxationMethodMatrix)
            {
                faktor =  double.Parse(b.Replace('.', ','));
                Tuple<List<double[,]>,List<double[,]>,List<double>> ret = linear.SORMethodMatrix(soustava, vektorB, faktor, pocatecni_aproximace, pocetIter, presnost);
                result = VypisSorMetodaMatice(ret);
            }
            else if (method == Methods.SuperRelaxationMethodFolder)
            {
                faktor = double.Parse(b.Replace('.', ','));
                LinearniRovniceSlozky_ret ret = linear.SORMethodFolder(soustava, vektorB, pocatecni_aproximace, faktor, pocetIter, presnost);
                result = VypisSORMetodaSlozka(ret);
            }
            sw.WriteLine(GenerateHtml(result));
            sw.Close();
        }

        private void NameWindow()
        {
            if (method == Methods.GaussElimination)
            {
                this.Text = "Gaussova eliminace";
                formula = Formulas.GaussElimination;
            }
            else if (method == Methods.LUDecomposition)
            {
                this.Text = "LU rozklad";
                formula = Formulas.LUDecomposition;
            }
            else if (method == Methods.CholeskyDecomposotion)
            {
                this.Text = "Choleského dekompozice";
                formula = Formulas.CholeskyDecomposition;
            }
            else if (method == Methods.JacobiMethodFolder)
            {
                this.Text = "Jacobiho metoda (složkový tvar)";
                formula = Formulas.JacobiMethodFolder;
            }
            else if (method == Methods.JacobiMethodMatrix)
            {
                this.Text = "Jacobiho metoda (maticový tvar)";
                formula = Formulas.JacobiMethodMatrix;
            }
            else if (method == Methods.GaussSeidelMethodFolder)
            {
                this.Text = "Gauss-Seidelova metoda (složkový tvar)";
                formula = Formulas.GaussSeidelMethodFolder;
            }
            else if (method == Methods.GaussSeidelMethodMatrix)
            {
                this.Text = "Gauss-Seidelova metoda (maticový tvar)";
                formula = Formulas.GaussSeidelMethodMatrix;
            }
            else if (method == Methods.SuperRelaxationMethodFolder)
            {
                this.Text = "Super relaxační metoda (složkový tvar)";
                formula = Formulas.SORMethodFolder;
            }
            else if (method == Methods.SuperRelaxationMethodMatrix)
            {
                this.Text = "Super relaxační metoda (maticový tvar)";
                formula = Formulas.SORMethodMatrix;
            }
            NameMethod = this.Text;
        }

        private void RemoveHtmlInstance()
        {
            string directory = Directory.GetCurrentDirectory();
            List<string> files = Directory.GetFiles(directory).ToList();
            foreach (string file in files)
            {
                if (file.Contains(temp + instanceId + method.ToString()))
                {
                    File.Delete(Path.GetFullPath(file));
                }
            }
        }

        private void DisableComponents()
        {
            if ((int)method > 6)
            {
                faktor_label.Visible = true;
                faktor_textBox.Visible = true;
            }
            if ((int)method < 3)
            {
                Presnost_label.Visible = false;
                Presnost_textBox.Visible = false;
                Pocet_iteraci_label.Visible = false;
                Pocet_iteraci_textBox.Visible = false;
                pocatecni_aproximace_dataGridView.Visible = false;
                Pocatecni_aproximace_label.Visible = false;
            }
        }

        private void AlignComponents()
        {
            if ((int)method < 3)
            {
                Decimal_places_label.Location = Pocet_iteraci_label.Location;
                DecimalPlaces_numericUpDown.Location = Pocet_iteraci_textBox.Location;
                FontSize_label.Location = faktor_label.Location;
                FontSize_trackBar.Location = faktor_textBox.Location;
            }
            else if ((int)method >= 3 && (int)method < 7)
            {
                FontSize_label.Location = Decimal_places_label.Location;
                FontSize_trackBar.Location = DecimalPlaces_numericUpDown.Location;
                Decimal_places_label.Location = Presnost_label.Location;
                DecimalPlaces_numericUpDown.Location = Presnost_textBox.Location;
                Presnost_label.Location = faktor_label.Location;
                Presnost_textBox.Location = faktor_textBox.Location;
            }
        }

        private string GenerateHtml(string input)
        {
            string currDirectory = Directory.GetCurrentDirectory();
            StringBuilder builder = new StringBuilder();
            builder.Append(Html.Div() + Html.Span("Byla zadána soustava:") + Mathml.HeaderMath(Mathml.Display.inline) + Mathml.AdvMatrix(GetDataOfDataGridView(Soustava_DatagridView),GetDataOfDataGridView(VektorB_dataGridView),expression,Mathml.TypeBracket.kulate) + Mathml.EndMath);
            if ((int)method > 3)
                builder.Append(Html.Span(" s počátečním vektorem:") + Mathml.HeaderMath(Mathml.Display.inline) + Mathml.Matrix(GetDataOfDataGridView(pocatecni_aproximace_dataGridView), expression, Mathml.TypeBracket.kulate) + Mathml.EndMath);
            if ((int)method > 6)
                builder.Append(Html.Span(" a faktorem ") + Mathml.HeaderMath(Mathml.Display.inline)+Mathml.Literal(Symbols.omega)+Mathml.EndMath+ Html.Span(": ") + Mathml.HeaderMath(Mathml.Display.inline) + expression.TextToMathml(faktor_textBox.Text, Mathml.Display.inline) + Mathml.EndMath);
            if ((int)method >= 3)
            {
                builder.Append(Html.Br + Html.Span(" Požadovaná přesnost : ") + Mathml.HeaderMath(Mathml.Display.inline) + expression.TextToMathml(Presnost_textBox.Text, Mathml.Display.inline) + Mathml.EndMath);
                builder.Append(Html.Br + Html.Span(" Maximální počet iterací : ") + Mathml.HeaderMath(Mathml.Display.inline) + expression.TextToMathml(Pocet_iteraci_textBox.Text, Mathml.Display.inline) + Mathml.EndMath);
            }
            builder.Append(Html.Br);
            builder.Append(Html.Span("Zvolená metoda:   ") + Html.Span(NameMethod) + Html.Br);
            builder.Append(Html.Span("Předpis zvolené metody:") + Html.Br + formula + Html.Br);
            builder.Append(Html.Span("Výsledek")+Html.Br+Html.EndDiv);
            input = Html.Div() + input + Html.EndDiv;
            result = Html.HeaderHTML(NameMethod) + builder.ToString() + input + Html.EndHTML;
            return result;  
        }

        private bool CheckInput(bool RunWorker)
        {
            this.RemoveSpacesFromTextBoxs();
            string checkTextBoxsInfinity = this.CheckTextBoxsInfinity(expression);
            if (checkTextBoxsInfinity != string.Empty)
            {
                MessageBox.Show("Ve vstupních datech je nastavena nedefinovaná hodnota (" + (checkTextBoxsInfinity) + ")", "Nedefinovaná hodnota", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
            if (Pocet_iteraci_textBox.Text != string.Empty)
            {
                if (expression.ComputeRPN(expression.RPN(Pocet_iteraci_textBox.Text)) <= 0 || (Math.Floor(expression.ComputeRPN(expression.RPN(Pocet_iteraci_textBox.Text))) != expression.ComputeRPN(expression.RPN(Pocet_iteraci_textBox.Text))))
                {
                    MessageBox.Show("Počet iterací musí být kladné přirozené číslo", "Počet iterací", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                    VypocitejButton.Enabled = true;
                    DecimalPlaces_numericUpDown.Enabled = true;
                    return true;
                }
                if (!reg.Check(Pocet_iteraci_textBox.Text))
                {
                    MessageBox.Show("Špatně zadaný počet iterací", "Počet iterací", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                    VypocitejButton.Enabled = true;
                    DecimalPlaces_numericUpDown.Enabled = true;
                    return true;
                }
            }
            if (!reg.Check(Presnost_textBox.Text))
            {
                MessageBox.Show("Špatně zadaná přesnost", "Přesnost", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
            if (!reg.Check(faktor_textBox.Text))
            {
                MessageBox.Show("Špatně zadaný faktor", "Faktor", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true; ;
            }
            foreach (DataGridView datagrid in this.GetAll(typeof(DataGridView)))
            {
                string[,] dataOfGrid = GetDataOfDataGridView(datagrid);
                for (int i = 0; i < dataOfGrid.GetLength(0); i++)
                {
                    for (int j = 0; j < dataOfGrid.GetLength(1); j++)
                    {
                        if (!reg.Check(dataOfGrid[i,j]))
                        {
                            MessageBox.Show("Špatně zadaný prvek na pozici (" + (i+1) + "," + (j+1) + ")" + "-"+datagrid.Tag.ToString(), "Chyba ve vstupu", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            VypocitejButton.Enabled = true;
                            DecimalPlaces_numericUpDown.Enabled = true;
                            return true;
                        }
                        if (dataOfGrid[i, j] == string.Empty || dataOfGrid[i, j].Contains('x'))
                        {
                            MessageBox.Show("Nezadaný prvek na pozici (" + (i+1) + "," + (j+1) + ")" + "-" + datagrid.Tag.ToString(), "Chyba ve vstupu", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            VypocitejButton.Enabled = true;
                            DecimalPlaces_numericUpDown.Enabled = true;
                            return true;
                        }
                        if (double.IsInfinity(expression.ComputeRPN(expression.RPN(dataOfGrid[i, j]))) ||
                            double.IsNaN(expression.ComputeRPN(expression.RPN(dataOfGrid[i, j]))))
                        {
                            MessageBox.Show("Ve vstupních datech je nastavena nedefinovaná hodnota (" + (datagrid.Tag.ToString()) + ") na pozici :(" + i + ","
                                + j + ")", "Nedefinovaná hodnota", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            VypocitejButton.Enabled = true;
                            DecimalPlaces_numericUpDown.Enabled = true;
                            return true;
                        }
                    }
                }
            }
            if (!incorrect && RunWorker)
                backgroundWorker1.RunWorkerAsync();
            return false;
        }

        private void CheckFillTextBoxs()
        {
            
            if ((int)method >= 3)
            {
                if (Pocet_iteraci_textBox.Text == string.Empty || Presnost_textBox.Text == string.Empty)
                {
                    AddTask_Button.Enabled = false;
                    VypocitejButton.Enabled = false;
                    return;
                }
            }
            if ((int)method > 6)
            {
                if (faktor_textBox.Text == string.Empty)
                {
                    AddTask_Button.Enabled = false;
                    VypocitejButton.Enabled = false;
                    return;
                }
            }
            var c = this.GetAll(typeof(DataGridView));
            List<Control> controls = c.ToList();
            for (int i = 0; i < controls.Count(); i++)
            {
                if ((controls[i] is DataGridView) && controls[i].Visible)
                {
                    string a = controls[i].Name;
                    DataGridView dgv = (DataGridView)controls[i];
                    for (int j = 0; j < dgv.ColumnCount; j++)
                        for (int k = 0; k < dgv.RowCount; k++)
                            if (dgv[j, k].Value == null || dgv[j, k].Value.ToString() == string.Empty)
                            {
                                VypocitejButton.Enabled = false;
                                AddTask_Button.Enabled = false;
                                return;
                            }
                }
            }
            AddTask_Button.Enabled = true;
            VypocitejButton.Enabled = true;
        }

        private void pocetPromennych_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            CheckFillTextBoxs();
            Soustava_label.Visible = true;
            Soustava_DatagridView.Visible = true;
            VektorB_dataGridView.Visible = true;
            Soustava_DatagridView.ColumnCount = Soustava_DatagridView.RowCount =
            VektorB_dataGridView.RowCount  = (int)((NumericUpDown)sender).Value;
            Soustava_DatagridView.Size = GetSizeDataGridView(Soustava_DatagridView);
            VektorB_dataGridView.Size = GetSizeDataGridView(VektorB_dataGridView);
            VektorB_dataGridView.Location = new Point(Soustava_DatagridView.Location.X + Soustava_DatagridView.Width, Soustava_DatagridView.Location.Y);
            if (pocatecni_aproximace_dataGridView.Visible)
            {
                pocatecni_aproximace_dataGridView.RowCount = (int)((NumericUpDown)sender).Value;
                pocatecni_aproximace_dataGridView.Size = GetSizeDataGridView(pocatecni_aproximace_dataGridView);
            /*    pocatecni_aproximace_dataGridView.Location = new Point(VektorB_dataGridView.Location.X + VektorB_dataGridView.Width + offset,
                    VektorB_dataGridView.Location.Y);
                Pocatecni_aproximace_label.Location = new Point(pocatecni_aproximace_dataGridView.Location.X - ((offset-Pocatecni_aproximace_label.Width) / 2 +Pocatecni_aproximace_label.Width),
                    pocatecni_aproximace_dataGridView.Location.Y + (pocatecni_aproximace_dataGridView.Height / 2 - Pocatecni_aproximace_label.Height / 2));*/
                pocatecni_aproximace_dataGridView.Location = new Point(Soustava_DatagridView.Location.X,Soustava_DatagridView.Location.Y+Soustava_DatagridView.Height + offset/2 );
                Pocatecni_aproximace_label.Location = new Point(Soustava_label.Location.X, pocatecni_aproximace_dataGridView.Location.Y + (pocatecni_aproximace_dataGridView.Height / 2 - Pocatecni_aproximace_label.Height / 2));
            }
            Soustava_label.Location = new Point(Soustava_DatagridView.Location.X - ((offset - Soustava_label.Width) / 2 + Soustava_label.Width),
                Soustava_DatagridView.Location.Y + (Soustava_DatagridView.Height / 2 - Soustava_label.Height / 2));
            if ((VektorB_dataGridView.Location.X + VektorB_dataGridView.Width) < splitContainer1.Panel2.Width + offset / 2)
            {
                if ((VektorB_dataGridView.Location.X + VektorB_dataGridView.Width + offset / 2) > ChangeMethod_GroupBox.Location.X + ChangeMethod_GroupBox.Width)
                    splitContainer1.SplitterDistance = VektorB_dataGridView.Location.X + VektorB_dataGridView.Width + offset / 2;
            }
            else
                splitContainer1.SplitterDistance = splitContainer1.Width / 2 + offset / 2;
       }

        private Size GetSizeDataGridView(DataGridView dgv)
        {
            string[,] dataOfDataGrid = GetDataOfDataGridView(dgv);
            int rowCount = dgv.RowCount;
            int columnCount = dgv.ColumnCount;
            int sumHeight = 3;
            int sumWidth = 3;
            int HeightRow = 30;
            int WidthRow = 100;
            dgv.Rows.Clear();
            dgv.Columns.Clear();
            
            for (int i = 0; i < columnCount; i++)
            {
                DataGridViewColumn col = new DataGridViewColumn();
                col.Width = WidthRow;
                col.CellTemplate = new DataGridViewTextBoxCell();
                sumWidth += col.Width;
                dgv.Columns.Add(col);
            }
            for (int i = 0; i < rowCount; i++)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.Height = HeightRow;
                sumHeight += row.Height;
                dgv.Rows.Add(row);
            }
            for (int i = 0; i < columnCount; i++)
            {
                for (int j = 0; j < rowCount; j++)
                {
                    dgv[i, j].Value = dataOfDataGrid[j, i]; 
                }
            }
            return new Size(sumWidth, sumHeight);
        }

        private string[,] GetDataOfDataGridView(DataGridView dgv)
        {
            string[,] result = new string[dgv.Rows.Count, dgv.Columns.Count];
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                for (int j = 0; j < dgv.Columns.Count; j++)
                {
                    if (dgv[j, i].Value != null)
                        result[i, j] = dgv[j, i].Value.ToString();
                    else
                        result[i, j] = string.Empty;
                }
            }
            return result;
        }

        private double[,] GetDoubleDataOfDataGridView(DataGridView dgv)
        {
            double[,] result = new double[dgv.Rows.Count, dgv.Columns.Count];
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                for (int j = 0; j < dgv.Columns.Count; j++)
                {
                    string tmp = (string)dgv[j, i].Value;
                        result[i, j] = expression.ComputeRPN(expression.RPN(tmp));
                }
            }
            return result;
        }

        private void InsertDataToDataGridView(string[,] input, DataGridView destination)
        {
            destination.ColumnCount = input.GetLength(1);
            destination.RowCount = input.GetLength(0);
            destination.Size = GetSizeDataGridView(destination);
            for (int i = 0; i < destination.ColumnCount; i++)
            {
                for (int j = 0; j < destination.RowCount; j++)
                    destination[i, j].Value = input[j, i];
            }
        }

        private void xMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (save.SaveAsXML(result))
            {
                toolStripStatusLabel1.Text = Path.GetFileName(save.PathToDestinationFileProperty) + " soubor byl vytvořen";
                PathToLastExportFile = save.PathToDestinationFileProperty;
                toolStripSplitButton1.Visible = true;
            }
        }

        private void hTMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (save.SaveAsHTML(result))
            {
                toolStripStatusLabel1.Text = Path.GetFileName(save.PathToDestinationFileProperty) + " soubor byl vytvořen";
                PathToLastExportFile = save.PathToDestinationFileProperty;
                toolStripSplitButton1.Visible = true;
            }
        }

        private void imageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (result != string.Empty)
            {
                int width = int.Parse(webControl1.ExecuteJavascriptWithResult("WidthDiv()"));
                int height = int.Parse(webControl1.ExecuteJavascriptWithResult("HeightDiv()"));
                int fontSize = FontSize_trackBar.Value;
                if (save.SaveAsImage(result, width, height, fontSize, pathToHtml))
                {
                    toolStripStatusLabel1.Text = Path.GetFileName(save.PathToDestinationFileProperty) + " soubor byl vytvořen";
                    PathToLastExportFile = save.PathToDestinationFileProperty;
                    toolStripSplitButton1.Visible = true;
                }
            }
        }

        private void FontSize_trackBar_MouseUp(object sender, MouseEventArgs e)
        {
            string size = FontSize_trackBar.Value.ToString();
            webControl1.ExecuteJavascript("changeSize('" + size + "px')");
        }

        private void FontSize_trackBar_KeyUp(object sender, KeyEventArgs e)
        {
            string size = FontSize_trackBar.Value.ToString();
            webControl1.ExecuteJavascript("changeSize('" + size + "px')");
        }

        private void pDFToolStripMenuItem_Click(object sender, EventArgs e)
        {
          /*  if (result != string.Empty)
            {
                int width = int.Parse(webControl1.ExecuteJavascriptWithResult("WidthDiv()"));
                int height = int.Parse(webControl1.ExecuteJavascriptWithResult("HeightDiv()"));
                int fontSize = FontSize_trackBar.Value;
                if (save.SaveAsPDF(result, width, height, fontSize))
                {
                    toolStripStatusLabel1.Text = Path.GetFileName(save.PathToDestinationFileProperty) + " soubor byl vytvořen";
                    PathToLastExportFile = save.PathToDestinationFileProperty;
                    toolStripSplitButton1.Visible = true;
                }
            }*/
        }

        private void DecimalPlaces_ValueChanged(object sender, EventArgs e)
        {
            Extension.countDecimalPlaces = (int)DecimalPlaces_numericUpDown.Value;
        }

        private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
        {
            Process.Start(PathToLastExportFile);

        }

        private void Awesomium_Windows_Forms_WebControl_DocumentReady(object sender, UrlEventArgs e)
        {
            FontSize_trackBar.Enabled = true;
            while (!webControl1.ExecuteJavascriptWithResult("IsCompleted()")) ;
            exportyToolStripMenuItem.Enabled = true;
        }

        private void toolStripSplitButton1_MouseHover(object sender, EventArgs e)
        {
            statusStrip.ShowItemToolTips = true;
            toolStripSplitButton1.ToolTipText = "otevřít soubor";
        }

        private void Linear_solutions_win_FormClosing(object sender, FormClosingEventArgs e)
        {
            RemoveHtmlInstance();
        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {
            
          /*  Match match = Regex.Match(result, "font-size:\\s*[0-9]*px");
            result = result.Replace(match.ToString(), "font-size:" + FontSize_trackBar.Value + "px");
            this.sw = new StreamWriter(pathToHtml);
            sw.WriteLine(result);
            sw.Close();
            webControl1.Source = new Uri(Path.GetFullPath(pathToHtml));*/
        }

        private string VypisGaussovaEliminace(Tuple<List<double[,]>, List<double[,]>,List<double>,double[,]> vstup)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Html.Span("V " + 1 + ". sloupci nalezneme prvek s největší absolutní hodnotou. Řádek s tímto prvkem zaměníme s " + 1 +". řádkem.", " style=\"margin:auto; display:table;\""));
            sb.Append(Mathml.HeaderMath(Mathml.Display.block));
            sb.Append(Mathml.AdvMatrix(vstup.Item1[1], vstup.Item2[1], Mathml.TypeBracket.kulate));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.EndMath);
            sb.Append(Html.Br);
           
            int increment = vstup.Item1[0].GetLength(0) - 1;
            int offset = 0;
            for (int i = 0; i < vstup.Item1[0].GetLength(1); i++)
            {
                int j = 0;
                while (j < increment)
                {
                    try
                    {
                        sb.Append(Html.Span((i + 1) + ". řádek vynásobíme "));
                        sb.Append(Mathml.HeaderMath(Mathml.Display.inline));
                        sb.Append(Mathml.Row(Mathml.Frac(Mathml.Sub(Mathml.Literal("a"), Mathml.Constant((j+2+i )) + Mathml.Constant(i+1)),
                                            Mathml.Sub(Mathml.Literal("a"), Mathml.Constant((i + 1)) + Mathml.Constant(i + 1)))));
                        sb.Append(Mathml.LineBreak);
                        sb.Append(Mathml.EndMath);
                        sb.Append(Html.Span(" a odečteme od " + (j + 2+i) + ". řádku"));
                        sb.Append(Html.Br);
                        sb.Append(Mathml.HeaderMath(Mathml.Display.block));
                        sb.Append(Mathml.Row(Mathml.Frac(Mathml.Sub(Mathml.Literal("a"), Mathml.Constant((j + 2 + i)) + Mathml.Constant(i + 1)),
                                            Mathml.Sub(Mathml.Literal("a"), Mathml.Constant((i + 1)) + Mathml.Constant(i + 1))))
                                            + Mathml.Equal
                                            + Mathml.Frac((vstup.Item1[i + j + 1 + offset][(j + 1 + i),(i)]).NumToMathml(),(vstup.Item1[i + j + 1 + offset][(i),(i)]).NumToMathml())
                                            + Mathml.Equal
                                            + vstup.Item3[j+offset].NumToMathml()
                            );
                        sb.Append(Mathml.LineBreak);
                        sb.Append(Mathml.EndMath);
                        sb.Append(Html.Br);
                        sb.Append(Mathml.HeaderMath(Mathml.Display.block));
                        sb.Append(Mathml.AdvMatrix(vstup.Item1[i + j + 2 + offset], vstup.Item2[i + j + 2 + offset]));
                        sb.Append(Mathml.EndMath);
                        sb.Append(Html.Br);
                        if ((i + j + 2 + offset) + 1 == vstup.Item1.Count)
                        {

                            sb.Append(Html.Span("Zpětným chodem určíme výsledný vektor řešení."));
                            sb.Append(Mathml.HeaderMath(Mathml.Display.block));
                            for (int k = vstup.Item4.Length - 1; k > -1; k--)
                            {
                                sb.Append(Mathml.Sub(Mathml.Literal("x"),Mathml.Constant(k+1)));
                                sb.Append(Mathml.Equal);
                                sb.Append(Mathml.Frac(Mathml.Constant(1),vstup.Item1.Last()[k, k].NumToMathml()));
                                sb.Append(Mathml.Multiply);
                                sb.Append(Mathml.LZ);
                                sb.Append(vstup.Item2.Last()[k,0].NumToMathml());
                                for (int l = k + 1; l < vstup.Item2.Last().GetLength(0); l++)
                                {
                                    if (l == k + 1)
                                    {
                                        sb.Append(Mathml.Minus + Mathml.LZ);
                                        sb.Append(vstup.Item1.Last()[k, l].NumToMathml());
                                    }
                                    else
                                        sb.Append(vstup.Item1.Last()[k, l].NumToMathml("+", "+"));
                                    sb.Append(Mathml.Multiply);
                                    sb.Append(Mathml.Sub(Mathml.Literal("x"), Mathml.Constant(l+1)));
                                    if (l + 1 == vstup.Item2.Last().GetLength(0))
                                        sb.Append(Mathml.PZ);
                                }
                                sb.Append(Mathml.PZ);
                                sb.Append(Mathml.Equal);
                                sb.Append(vstup.Item4[k,0].NumToMathml());
                                sb.Append(Mathml.LineBreak);
                            }
                            sb.Append(Mathml.EndMath);
                            sb.Append(Html.Br);
                            sb.Append(Mathml.HeaderMath(Mathml.Display.block));
                            sb.Append(Mathml.LineBreak);
                            sb.Append(Mathml.Row(Mathml.Text("vektor x") + Mathml.Equal + Mathml.Matrix(vstup.Item4, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
                            sb.Append(Mathml.EndMath);
                            return sb.ToString();
                        }
                    }
                    catch
                    {}
                    j++;
                }
                try
                {
                    sb.Append(Html.Span("V " + (i + 2) + ". sloupci nalezneme prvek s největší absolutní hodnotou. Řádek s tímto prvkem zaměníme s " + (i+2) + ". řádkem."));
                    sb.Append(Mathml.HeaderMath(Mathml.Display.block));
                    sb.Append(Mathml.AdvMatrix(vstup.Item1[i + j + 2 + offset], vstup.Item2[i + j + 2 + offset]));
                    sb.Append(Mathml.LineBreak);
                    sb.Append(Mathml.EndMath);
                    sb.Append(Html.Br);
                    if ((i + j + 2 + offset) + 1 == vstup.Item1.Count)
                    {
                        sb.Append(Html.Span("Zpětným chodem určíme výsledný vektor řešení."));
                        sb.Append(Mathml.HeaderMath(Mathml.Display.block));
                        for (int k = vstup.Item4.Length - 1; k > -1; k--)
                        {
                            sb.Append(Mathml.Sub(Mathml.Literal("x"), Mathml.Constant(k + 1)));
                            sb.Append(Mathml.Equal);
                            sb.Append(Mathml.Frac(Mathml.Constant(1), vstup.Item1.Last()[k, k].NumToMathml()));
                            sb.Append(Mathml.Multiply);
                            sb.Append(Mathml.LZ);
                            sb.Append(vstup.Item2.Last()[k, 0].NumToMathml());
                            for (int l = k + 1; l < vstup.Item2.Last().GetLength(0); l++)
                            {
                                if (l == k + 1)
                                {
                                    sb.Append(Mathml.Minus + Mathml.LZ);
                                    sb.Append(vstup.Item1.Last()[k, l].NumToMathml());
                                }
                                else
                                    sb.Append(vstup.Item1.Last()[k, l].NumToMathml("+", "+"));
                                sb.Append(Mathml.Multiply);
                                sb.Append(Mathml.Sub(Mathml.Literal("x"), Mathml.Constant(l + 1)));
                                if (l + 1 == vstup.Item2.Last().GetLength(0))
                                    sb.Append(Mathml.PZ);
                            }
                            sb.Append(Mathml.PZ);
                            sb.Append(Mathml.Equal);
                            sb.Append(vstup.Item4[k, 0].NumToMathml());
                            sb.Append(Mathml.LineBreak);
                        }
                        sb.Append(Mathml.EndMath);
                        sb.Append(Html.Br);
                        sb.Append(Mathml.HeaderMath(Mathml.Display.block));
                        sb.Append(Mathml.LineBreak);
                        sb.Append(Mathml.Row(Mathml.Text("vektor x") + Mathml.Equal + Mathml.Matrix(vstup.Item4, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
                        sb.Append(Mathml.EndMath);
                        return sb.ToString();
                    }
                }
                catch
                {}
                offset += increment;
                increment--;
            }
                sb.Append(Html.Span("Zpětným chodem určíme výsledný vektor řešení."));
                sb.Append(Mathml.HeaderMath(Mathml.Display.block));
                for (int k = vstup.Item4.Length - 1; k > -1; k--)
                {
                    sb.Append(Mathml.Sub(Mathml.Literal("x"), Mathml.Constant(k + 1)));
                    sb.Append(Mathml.Equal);
                    sb.Append(Mathml.Frac(Mathml.Constant(1), vstup.Item1.Last()[k, k].NumToMathml()));
                    sb.Append(Mathml.Multiply);
                    sb.Append(Mathml.LZ);
                    sb.Append(vstup.Item2.Last()[k, 0].NumToMathml());
                    for (int l = k + 1; l < vstup.Item2.Last().GetLength(0); l++)
                    {
                        if (l == k + 1)
                        {
                            sb.Append(Mathml.Minus + Mathml.LZ);
                            sb.Append(vstup.Item1.Last()[k, l].NumToMathml());
                        }
                        else
                            sb.Append(vstup.Item1.Last()[k, l].NumToMathml("+", "+"));
                        sb.Append(Mathml.Multiply);
                        sb.Append(Mathml.Sub(Mathml.Literal("x"), Mathml.Constant(l + 1)));
                        if (l + 1 == vstup.Item2.Last().GetLength(0))
                            sb.Append(Mathml.PZ);
                    }
                    sb.Append(Mathml.PZ);
                    sb.Append(Mathml.Equal);
                    sb.Append(vstup.Item4[k, 0].NumToMathml());
                    sb.Append(Mathml.LineBreak);
                }
                sb.Append(Mathml.EndMath);
                sb.Append(Html.Br);
                sb.Append(Mathml.HeaderMath(Mathml.Display.block));
                sb.Append(Mathml.LineBreak);
                sb.Append(Mathml.Row(Mathml.Text("vektor x") + Mathml.Equal + Mathml.Matrix(vstup.Item4, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
                sb.Append(Mathml.EndMath);
                return sb.ToString(); 
        }

        private string VypisLURozklad(LinearniRovnice_ret vstup)
        {
            StringBuilder sb = new StringBuilder(Mathml.HeaderMath());
            StringBuilder sb1 = new StringBuilder();
            for (int i = 0; i < vstup.matrixProperty.GetLength(1); i++)
            {
                sb.Append(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(i) + Mathml.Literal(",") + Mathml.Constant(i)) + Mathml.Equal + Mathml.Constant(1));
                sb.Append(Mathml.LineBreak);
            }
            for (int i = 0; i < vstup.matrixProperty.GetLength(0); i++)
                for (int j = 0; j < vstup.matrixProperty.GetLength(1); j++)
                {
                    if (i <= j)
                    {
                        sb.Append(Mathml.Sub(Mathml.Literal("u"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                        sb.Append(Mathml.Equal + Mathml.Sub(Mathml.Literal("a"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                        for (int k = 0; k < i; k++)
                        {
                            if (k == 0)
                                sb.Append(Mathml.Minus + Mathml.LZ);
                            sb.Append(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(k + 1)) + Mathml.Multiply +
                                Mathml.Sub(Mathml.Literal("u"), Mathml.Constant(k + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                            if (k + 1 != i)
                                sb.Append(Mathml.Plus);
                            else
                                sb.Append(Mathml.PZ);
                        }
                        sb.Append(Mathml.LineBreak);
                    }
                    else
                    {
                        sb.Append(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                        sb.Append(Mathml.Equal);
                        sb1.Clear();
                        sb1.Append(Mathml.Sub(Mathml.Literal("a"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                        for (int k = 0; k < j; k++)
                        {
                            if (k == 0)
                                sb1.Append(Mathml.Minus + Mathml.LZ);
                            sb1.Append(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(k + 1)));
                            sb1.Append(Mathml.Multiply + Mathml.Sub(Mathml.Literal("u"), Mathml.Constant(k + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                            if (k + 1 != j)
                                sb1.Append(Mathml.Plus);
                            else
                                sb1.Append(Mathml.PZ);
                        }
                        sb.Append(Mathml.Frac(sb1.ToString(), Mathml.Sub(Mathml.Literal("u"), Mathml.Constant(j + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1))));
                        sb.Append(Mathml.LineBreak);
                    }
                }
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.EndMath);
            sb.Append(Mathml.HeaderMath());
            for (int i = 0; i < vstup.matrixLProperty.GetLength(1); i++)
            {
                sb.Append(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(i) + Mathml.Literal(",") + Mathml.Constant(i)) + Mathml.Equal + Mathml.Constant(1));
                sb.Append(Mathml.LineBreak);
            }
            for (int i = 0; i < vstup.matrixProperty.GetLength(0); i++)
                for (int j = 0; j < vstup.matrixProperty.GetLength(1); j++)
                {
                    if (i <= j)
                    {
                        sb.Append(Mathml.Sub(Mathml.Literal("u"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                        sb.Append(Mathml.Equal + vstup.matrixProperty[i, j].NumToMathml());
                        for (int k = 0; k < i; k++)
                        {
                            if (k == 0)
                                sb.Append(Mathml.Minus + Mathml.LZ);
                            sb.Append(vstup.matrixLProperty[i, k].NumToMathml() + Mathml.Multiply + vstup.matrixUProperty[k, j].Bracket());
                            if (k + 1 != i)
                                sb.Append(Mathml.Plus);
                            else
                                sb.Append(Mathml.PZ);
                            if (k + 1 == i)
                                sb.Append(Mathml.Equal + vstup.matrixUProperty[i, j].NumToMathml());
                        }
                        sb.Append(Mathml.LineBreak);
                    }
                    else
                    {
                        sb.Append(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                        sb.Append(Mathml.Equal);
                        sb1.Clear();
                        sb1.Append(vstup.matrixProperty[i, j].NumToMathml());
                        for (int k = 0; k < j; k++)
                        {
                            if (k == 0)
                                sb1.Append(Mathml.Minus + Mathml.LZ);
                            sb1.Append(vstup.matrixLProperty[i, k].NumToMathml() + Mathml.Multiply + vstup.matrixUProperty[k, j].Bracket());
                            if (k + 1 != j)
                                sb1.Append(Mathml.Plus);
                            else
                                sb1.Append(Mathml.PZ);
                        }
                        sb.Append(Mathml.Frac(sb1.ToString(), vstup.matrixUProperty[j, j].NumToMathml()));
                        sb.Append(Mathml.Equal + vstup.matrixLProperty[i, j].NumToMathml());
                        sb.Append(Mathml.LineBreak);
                    }
                }
            sb.Append(Mathml.EndMath + Mathml.HeaderMath());
            sb.Append(Mathml.Row(Mathml.Text("Matice L") + Mathml.Equal + Mathml.Matrix(vstup.matrixLProperty, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Matice U") + Mathml.Equal + Mathml.Matrix(vstup.matrixUProperty, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Matice P") + Mathml.Equal + Mathml.Matrix(vstup.matrixPProperty, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Ly") + Mathml.Equal + Mathml.Text("Pb") + Mathml.Equal + Mathml.AdvMatrix(vstup.matrixLProperty, vstup.vectorBProperty, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Ux") + Mathml.Equal + Mathml.Text("y") + Mathml.Equal + Mathml.AdvMatrix(vstup.matrixUProperty, vstup.vectorYProperty, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("vektor x") + Mathml.Equal + Mathml.Matrix(vstup.vectorXProperty, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
            sb.Append(Mathml.EndMath);
            return sb.ToString();
        }

        private string VypisCholeskehoRozklad(LinearniRovnice_ret vstup)
        {
            StringBuilder sb = new StringBuilder(Mathml.HeaderMath());
            StringBuilder sb2 = new StringBuilder();
            StringBuilder sb1 = new StringBuilder();
            for (int i = 0; i < vstup.matrixProperty.GetLength(0); i++)
                for (int j = 0; j < vstup.matrixProperty.GetLength(1); j++)
                {
                    if (i == j)
                    {
                        sb.Append(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                        sb.Append(Mathml.Equal);
                        sb2.Clear();
                        sb2.Append(Mathml.Sub(Mathml.Literal("a"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                        for (int k = 0; k < i; k++)
                        {
                            if (k == 0)
                            {
                                sb2.Append(Mathml.Minus);
                                sb2.Append(Mathml.LZ);
                            }
                            sb2.Append(Mathml.Sup(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(k + 1)), Mathml.Constant(2)));
                            if (k + 1 != i)
                                sb2.Append(Mathml.Plus);
                            else
                                sb2.Append(Mathml.PZ);
                        }
                        sb.Append(Mathml.Sqrt(sb2.ToString()));
                        sb.Append(Mathml.LineBreak);
                    }
                    else if (i > j)
                    {
                        sb.Append(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                        sb.Append(Mathml.Equal);
                        sb1.Clear();
                        sb1.Append(Mathml.Sub(Mathml.Literal("a"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                        for (int k = 0; k < j; k++)
                        {

                            if (i > 0)
                            {
                                if (k == 0)
                                {
                                    sb1.Append(Mathml.Minus);
                                    sb1.Append(Mathml.LZ);
                                }
                                sb1.Append(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(k + 1)));
                                sb1.Append(Mathml.Multiply);
                                sb1.Append(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(j + 1) + Mathml.Literal(",") + Mathml.Constant(k + 1)));
                            }
                            if (k + 1 != j)
                                sb1.Append(Mathml.Plus);
                            else
                                sb1.Append(Mathml.PZ);
                        }
                        sb.Append(Mathml.Frac(sb1.ToString(), Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(j + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1))));
                        sb.Append(Mathml.LineBreak);
                    }
                }
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.EndMath);
            sb.Append(Html.Br);
            sb.Append(Mathml.HeaderMath());
            for (int i = 0; i < vstup.matrixProperty.GetLength(0); i++)
                for (int j = 0; j < vstup.matrixProperty.GetLength(1); j++)
                {
                    if (i == j)
                    {
                        sb.Append(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                        sb.Append(Mathml.Equal);
                        sb2.Clear();
                        sb2.Append(vstup.matrixProperty[j, j].NumToMathml());
                        for (int k = 0; k < i; k++)
                        {
                            if (k == 0)
                            {
                                sb2.Append(Mathml.Minus);
                                sb2.Append(Mathml.LZ);
                            }
                            sb2.Append(Mathml.Sup(vstup.matrixLProperty[i, k].NumToMathml(), Mathml.Constant(2)));
                            if (k + 1 != i)
                                sb2.Append(Mathml.Plus);
                            else
                                sb2.Append(Mathml.PZ);
                        }
                        sb.Append(Mathml.Sqrt(sb2.ToString()));
                        sb.Append(Mathml.Equal);
                        sb.Append(vstup.matrixLProperty[i, i].NumToMathml());
                        sb.Append(Mathml.LineBreak);
                    }
                    else if (i > j)
                    {
                        sb.Append(Mathml.Sub(Mathml.Literal("l"), Mathml.Constant(i + 1) + Mathml.Literal(",") + Mathml.Constant(j + 1)));
                        sb.Append(Mathml.Equal);
                        sb1.Clear();
                        sb1.Append(vstup.matrixProperty[i, j].NumToMathml());
                        for (int k = 0; k < j; k++)
                        {

                            if (i > 0)
                            {
                                if (k == 0)
                                {
                                    sb1.Append(Mathml.Minus);
                                    sb1.Append(Mathml.LZ);
                                }
                                sb1.Append(vstup.matrixLProperty[i, k].NumToMathml());
                                sb1.Append(Mathml.Multiply);
                                sb1.Append(vstup.matrixLProperty[j, k].NumToMathml());
                            }
                            if (k + 1 != j)
                                sb1.Append(Mathml.Plus);
                            else
                                sb1.Append(Mathml.PZ);
                        }
                        sb.Append(Mathml.Frac(sb1.ToString(), vstup.matrixLProperty[j, j].NumToMathml()));
                        sb.Append(Mathml.Equal);
                        sb.Append(vstup.matrixLProperty[i, j].NumToMathml());
                        sb.Append(Mathml.LineBreak);
                    }
                }
            sb.Append(Mathml.EndMath + Mathml.HeaderMath());
            sb.Append(Mathml.Row(Mathml.Text("Matice L") + Mathml.Equal + Mathml.Matrix(vstup.matrixLProperty, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Matice ") + Mathml.Sup(Mathml.Literal("L"), Mathml.Literal("t")) + Mathml.Equal + Mathml.Matrix(vstup.matrixUProperty, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Ly") + Mathml.Equal + Mathml.Literal("b") + Mathml.Equal + Mathml.AdvMatrix(vstup.matrixLProperty, vstup.vectorBProperty, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Sup(Mathml.Literal("L"), Mathml.Literal("t")) + Mathml.Literal("x") + Mathml.Equal + Mathml.Literal("y") + Mathml.Equal + Mathml.AdvMatrix(vstup.matrixUProperty, vstup.vectorYProperty, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("vektor x") + Mathml.Equal + Mathml.Matrix(vstup.vectorXProperty, Mathml.TypeBracket.kulate)) + Mathml.LineBreak);
            sb.Append(Mathml.EndMath);
            return sb.ToString();
        }

        private string VypisJacobihoMetodaSlozka(LinearniRovniceSlozky_ret vstup)
        {
            StringBuilder sb = new StringBuilder(Mathml.HeaderMath());
            StringBuilder sb1 = new StringBuilder();
            List<string> vyraz = new List<string>();
            int rows = vstup.matrixProperty.GetLength(0);
            int cols = vstup.matrixProperty.GetLength(1);
            for (int i = 0; i < rows; i++)
            {
                sb.Append(Mathml.SupSub(Mathml.Literal("x"), Mathml.Constant(i + 1), Mathml.Row(Mathml.LZ + Mathml.Literal("k") + Mathml.Plus + Mathml.Constant(1) + Mathml.PZ)) + Mathml.Equal);
                sb1.Clear();
                for (int j = 0; j < cols; j++)
                {
                    if (i != j)
                    {
                        if (sb1.ToString() == string.Empty)
                            sb1.Append(vstup.matrixProperty[i, j].NumToMathml());
                        else
                            sb1.Append((vstup.matrixProperty[i, j]).NumToMathml("+", "+"));
                        sb1.Append(Mathml.Multiply + Mathml.SupSub(Mathml.Literal("x"), Mathml.Constant(j + 1), Mathml.Row(Mathml.LZ + Mathml.Literal("k") + Mathml.PZ)));
                    }
                }
                if (cols > 1)
                {
                    sb.Append(Mathml.Frac(Mathml.Minus + Mathml.Constant(1) + Mathml.Multiply + Mathml.LZ + sb1.ToString() + Mathml.PZ, vstup.matrixProperty[i, i].NumToMathml()));
                    sb.Append(Mathml.Plus);
                }
                sb.Append(Mathml.Frac(vstup.vectorBProperty[i, 0].NumToMathml(), vstup.matrixProperty[i, i].NumToMathml()));
                sb.Append(Mathml.LineBreak);
            }
            sb.Append(Mathml.EndMath);
            int SizeTable = 10;
            int countIter = (int)Math.Ceiling((double)vstup.vectorsProperty.Count / 10);
            for (int i = 0; i < countIter; i++)
            {
                int start = 0 + i * SizeTable;
                int end;
                if (i + 1 != countIter)
                    end = 10;
                else
                    if (vstup.vectorsProperty.Count % 10 != 0)
                        end = vstup.vectorsProperty.Count % 10;
                    else
                        end = 10;
                sb.Append(Mathml.HeaderMath());
                if (i == 0)
                    sb.Append(Mathml.TableLinearFolder(vstup.vectorsProperty.GetRange(start, end), vstup.vectorsAccuracyProperty.GetRange(start, end - 1), true, i * 10));
                else
                    sb.Append(Mathml.TableLinearFolder(vstup.vectorsProperty.GetRange(start, end), vstup.vectorsAccuracyProperty.GetRange(start-1, end), false, i * 10));
                sb.Append(Mathml.EndMath);
            }
            return sb.ToString();
        }

        private string VypisGaussSeidelovaMetodaSlozka(LinearniRovniceSlozky_ret vstup)
        {
            StringBuilder sb = new StringBuilder(Mathml.HeaderMath());
            StringBuilder sb1 = new StringBuilder();
            List<string> vyraz = new List<string>();
            int rows = vstup.matrixProperty.GetLength(0);
            int cols = vstup.matrixProperty.GetLength(1);
            for (int i = 0; i < rows; i++)
            {
                sb.Append(Mathml.SupSub(Mathml.Literal("x"), Mathml.Constant(i + 1), Mathml.Row(Mathml.LZ + Mathml.Literal("k") + Mathml.Plus + Mathml.Constant(1) + Mathml.PZ)) + Mathml.Equal);
                sb1.Clear();
                for (int j = 0; j < cols; j++)
                {
                    if (i != j)
                    {
                        if (sb1.ToString() == string.Empty)
                            sb1.Append(vstup.matrixProperty[i, j].NumToMathml());
                        else
                            sb1.Append((vstup.matrixProperty[i, j]).NumToMathml("+", "+"));
                        if (i > j)
                        {
                            sb1.Append(Mathml.Multiply + Mathml.SupSub(Mathml.Literal("x"), Mathml.Constant(j + 1), Mathml.Row(Mathml.LZ + Mathml.Literal("k+1") + Mathml.PZ)));
                        }
                        else
                        {
                            sb1.Append(Mathml.Multiply + Mathml.SupSub(Mathml.Literal("x"), Mathml.Constant(j + 1), Mathml.Row(Mathml.LZ + Mathml.Literal("k") + Mathml.PZ)));
                        }
                    }
                }
                sb.Append(Mathml.Frac(Mathml.Minus + Mathml.Constant(1) + Mathml.Multiply + Mathml.LZ + sb1.ToString() + Mathml.PZ, vstup.matrixProperty[i, i].NumToMathml()));
                sb.Append(Mathml.Plus);
                sb.Append(Mathml.Frac(vstup.vectorBProperty[i, 0].NumToMathml(), vstup.matrixProperty[i, i].NumToMathml()));
                sb.Append(Mathml.LineBreak);
            }
            sb.Append(Mathml.EndMath);
            int SizeTable = 10;
            int countIter = (int)Math.Ceiling((double)vstup.vectorsProperty.Count / 10);
            for (int i = 0; i < countIter; i++)
            {
                int start = 0 + i * SizeTable;
                int end;
                if (i + 1 != countIter)
                    end = 10;
                else
                    if (vstup.vectorsProperty.Count % 10 != 0)
                        end = vstup.vectorsProperty.Count % 10;
                    else
                        end = 10;
                sb.Append(Mathml.HeaderMath());
                if (i == 0)
                    sb.Append(Mathml.TableLinearFolder(vstup.vectorsProperty.GetRange(start, end), vstup.vectorsAccuracyProperty.GetRange(start, end - 1), true, i * 10));
                else
                    sb.Append(Mathml.TableLinearFolder(vstup.vectorsProperty.GetRange(start, end), vstup.vectorsAccuracyProperty.GetRange(start - 1, end), false, i * 10)); sb.Append(Mathml.EndMath);
            }
            return sb.ToString();
        }

        private string VypisSORMetodaSlozka(LinearniRovniceSlozky_ret vstup)
        {
            StringBuilder sb = new StringBuilder(Mathml.HeaderMath());
            StringBuilder sb1 = new StringBuilder();
            List<string> vyraz = new List<string>();
            int rows = vstup.matrixProperty.GetLength(0);
            int cols = vstup.matrixProperty.GetLength(1);
            for (int i = 0; i < rows; i++)
            {
                sb.Append(Mathml.SupSub(Mathml.Literal("x"), Mathml.Constant(i + 1), Mathml.Row(Mathml.LZ + Mathml.Literal("k") + Mathml.Plus + Mathml.Constant(1) + Mathml.PZ)) + Mathml.Equal);
                sb1.Clear();
                sb.Append(Mathml.LZ + Mathml.Constant(1) + (-1 * vstup.factorProperty).NumToMathml("+", "+") + Mathml.PZ + Mathml.Multiply + Mathml.SupSub(Mathml.Literal("x"), Mathml.Constant(i + 1), Mathml.Row(Mathml.LZ + Mathml.Literal("k") + Mathml.PZ)));
                for (int j = 0; j < cols; j++)
                {
                    if (i != j)
                    {
                        if (sb1.ToString() == string.Empty)
                            sb1.Append(vstup.matrixProperty[i, j].NumToMathml());
                        else
                            sb1.Append((vstup.matrixProperty[i, j]).NumToMathml("+", "+"));
                        if (i > j)
                        {
                            sb1.Append(Mathml.Multiply + Mathml.SupSub(Mathml.Literal("x"), Mathml.Constant(j + 1), Mathml.Row(Mathml.LZ + Mathml.Literal("k+1") + Mathml.PZ)));
                        }
                        else
                        {
                            sb1.Append(Mathml.Multiply + Mathml.SupSub(Mathml.Literal("x"), Mathml.Constant(j + 1), Mathml.Row(Mathml.LZ + Mathml.Literal("k") + Mathml.PZ)));
                        }
                    }
                }
                sb1.Append((-1 * vstup.vectorBProperty[i, 0]).NumToMathml("+", "+"));
                sb.Append(Mathml.Minus + Mathml.Frac(Mathml.Constant(vstup.factorProperty) + Mathml.Multiply + Mathml.LZ + sb1.ToString() + Mathml.PZ, vstup.matrixProperty[i, i].NumToMathml()));
                sb.Append(Mathml.LineBreak);
            }
            sb.Append(Mathml.EndMath);
            int SizeTable = 10;
            int countIter = (int)Math.Ceiling((double)vstup.vectorsProperty.Count / 10);
            for (int i = 0; i < countIter; i++)
            {
                int start = 0 + i * SizeTable;
                int end;
                if (i + 1 != countIter)
                    end = 10;
                else
                    if (vstup.vectorsProperty.Count % 10 != 0)
                        end = vstup.vectorsProperty.Count % 10;
                    else
                        end = 10;
                sb.Append(Mathml.HeaderMath());
                if (i == 0)
                    sb.Append(Mathml.TableLinearFolder(vstup.vectorsProperty.GetRange(start, end), vstup.vectorsAccuracyProperty.GetRange(start, end - 1), true, i * 10));
                else
                    sb.Append(Mathml.TableLinearFolder(vstup.vectorsProperty.GetRange(start, end), vstup.vectorsAccuracyProperty.GetRange(start - 1, end), false, i * 10)); sb.Append(Mathml.EndMath);
            }
            return sb.ToString();
        }

        private string VypisJacobihoMetodaMatice(Tuple<List<double[,]>, List<double[,]>,List<double>> vstup)
        {
            StringBuilder sb = new StringBuilder(Mathml.HeaderMath(Mathml.Display.block));
            sb.Append(Mathml.Row(Mathml.Text("Matice ") + Mathml.Sup(Mathml.Literal("D"), Mathml.Row(Mathml.Minus + Mathml.Constant(1))) + Mathml.Equal + Mathml.Matrix(vstup.Item1[0], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Matice E") + Mathml.Equal + Mathml.Matrix(vstup.Item1[1], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Matice F") + Mathml.Equal + Mathml.Matrix(vstup.Item1[2], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Vektor b") + Mathml.Equal + Mathml.Matrix(vstup.Item1[3], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Vektor x") + Mathml.Equal + Mathml.Matrix(vstup.Item1[4], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("E+F") + Mathml.Equal + Mathml.Matrix(vstup.Item1[5], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Minus + Mathml.Sup(Mathml.Literal("D"), Mathml.Row(Mathml.Minus + Mathml.Constant(1))) + Mathml.LZ + Mathml.Text("E+F") + Mathml.PZ + Mathml.Equal + Mathml.Matrix(vstup.Item1[6], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Sup(Mathml.Literal("D"), Mathml.Row(Mathml.Minus + Mathml.Constant(1))) + Mathml.Multiply + Mathml.Text("b") + Mathml.Equal + Mathml.Matrix(vstup.Item1[7], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.EndMath);

            sb.Append(Mathml.HeaderMath(Mathml.Display.inline));
            sb.Append(Mathml.Row(Mathml.Sup(Mathml.Literal("x"), Mathml.Constant(0)) + Mathml.Equal + Mathml.Matrix(vstup.Item2[0], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.Space() + Mathml.Literal(Symbols.InvisibleTimes) + Mathml.LineBreak);
            sb.Append(Mathml.EndMath);
            sb.Append(Html.Br);
            for (int i = 1; i < vstup.Item2.Count; i++)
            {
                    sb.Append(Mathml.HeaderMath(Mathml.Display.inline));
                    sb.Append(Mathml.Row(Mathml.Sup(Mathml.Literal("x"), Mathml.Constant(i)) + Mathml.Equal + Mathml.Matrix(vstup.Item1[6], Mathml.TypeBracket.kulate) + Mathml.Multiply + Mathml.Matrix(vstup.Item2[i - 1], Mathml.TypeBracket.kulate) + Mathml.Plus + Mathml.Matrix(vstup.Item1[7], Mathml.TypeBracket.kulate) + Mathml.Equal + Mathml.Matrix(vstup.Item2[i], Mathml.TypeBracket.kulate)));
                    sb.Append(Mathml.LineBreak + Mathml.Literal(Symbols.euclidNorm) + Mathml.Sup(Mathml.Literal("x"), Mathml.Constant(i)) + Mathml.Minus + Mathml.Sup(Mathml.Literal("x"), Mathml.Constant(i - 1)) + Mathml.Literal(Symbols.euclidNorm) + Mathml.Equal + (vstup.Item3[i - 1]).NumToMathml());
                    sb.Append(Mathml.LineBreak);    
                    sb.Append(Mathml.EndMath);
                    sb.Append(Html.Br);
                
            }
            return sb.ToString();
        }

        private string VypisGaussSeidelMatice(Tuple<List<double[,]>, List<double[,]>,List<double>> vstup)
        {
            StringBuilder sb = new StringBuilder(Mathml.HeaderMath());
            sb.Append(Mathml.Row(Mathml.Text("Matice D") + Mathml.Equal + Mathml.Matrix(vstup.Item1[0], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Matice E") + Mathml.Equal + Mathml.Matrix(vstup.Item1[1], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Matice F") + Mathml.Equal + Mathml.Matrix(vstup.Item1[2], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Vektor b") + Mathml.Equal + Mathml.Matrix(vstup.Item1[3], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Vektor x") + Mathml.Equal + Mathml.Matrix(vstup.Item1[4], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("D+E") + Mathml.Equal + Mathml.Matrix(vstup.Item1[5], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Sup(Mathml.LZ + Mathml.Text("D+E") + Mathml.PZ, Mathml.Row(Mathml.Minus + Mathml.Constant(1))) + Mathml.Equal + Mathml.Matrix(vstup.Item1[6], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Sup(Mathml.LZ + Mathml.Text("D+E") + Mathml.PZ, Mathml.Row(Mathml.Minus + Mathml.Constant(1))) + Mathml.Multiply + Mathml.Text("F") + Mathml.Equal + Mathml.Matrix(vstup.Item1[7], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Sup(Mathml.LZ + Mathml.Text("D+E") + Mathml.PZ, Mathml.Row(Mathml.Minus + Mathml.Constant(1))) + Mathml.Multiply + Mathml.Text("b") + Mathml.Equal + Mathml.Matrix(vstup.Item1[8], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.EndMath);

            sb.Append(Mathml.HeaderMath(Mathml.Display.inline));
            sb.Append(Mathml.Row(Mathml.Sup(Mathml.Literal("x"), Mathml.Constant(0)) + Mathml.Equal + Mathml.Matrix(vstup.Item2[0], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.Space() + Mathml.Literal(Symbols.InvisibleTimes) + Mathml.LineBreak);
            sb.Append(Mathml.EndMath);
            sb.Append(Html.Br);
            for (int i = 1; i < vstup.Item2.Count; i++)
            {
                sb.Append(Mathml.HeaderMath(Mathml.Display.inline));
                sb.Append(Mathml.Row(Mathml.Sup(Mathml.Literal("x"), Mathml.Constant(i)) + Mathml.Equal + Mathml.Matrix(vstup.Item1[7], Mathml.TypeBracket.kulate) + Mathml.Multiply + Mathml.Matrix(vstup.Item2[i - 1], Mathml.TypeBracket.kulate) + Mathml.Plus + Mathml.Matrix(vstup.Item1[8], Mathml.TypeBracket.kulate) + Mathml.Equal + Mathml.Matrix(vstup.Item2[i], Mathml.TypeBracket.kulate)));
                sb.Append(Mathml.LineBreak + Mathml.Literal(Symbols.euclidNorm) + Mathml.Sup(Mathml.Literal("x"), Mathml.Constant(i)) + Mathml.Minus + Mathml.Sup(Mathml.Literal("x"), Mathml.Constant(i - 1)) + Mathml.Literal(Symbols.euclidNorm) + Mathml.Equal + (vstup.Item3[i - 1]).NumToMathml());
                sb.Append(Mathml.LineBreak);
                sb.Append(Mathml.EndMath);
                sb.Append(Html.Br);
            }
            return sb.ToString();
        }

        private string VypisSorMetodaMatice(Tuple<List<double[,]>, List<double[,]>,List<double>> vstup)
        {
            StringBuilder sb = new StringBuilder(Mathml.HeaderMath());
            sb.Append(Mathml.Row(Mathml.Text("Matice D") + Mathml.Equal + Mathml.Matrix(vstup.Item1[0], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Matice E") + Mathml.Equal + Mathml.Matrix(vstup.Item1[1], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Matice F") + Mathml.Equal + Mathml.Matrix(vstup.Item1[2], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Vektor b") + Mathml.Equal + Mathml.Matrix(vstup.Item1[3], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text("Vektor x") + Mathml.Equal + Mathml.Matrix(vstup.Item1[4], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text(Symbols.omega + Symbols.InvisibleTimes + Mathml.Literal("E")) + Mathml.Equal + Mathml.Matrix(vstup.Item1[5], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text(Symbols.omega + Symbols.InvisibleTimes + Mathml.Literal("F")) + Mathml.Equal + Mathml.Matrix(vstup.Item1[6], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Text(Symbols.omega + Symbols.InvisibleTimes + Mathml.Literal("b")) + Mathml.Equal + Mathml.Matrix(vstup.Item1[7], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.LZ + Mathml.Text("D" +Mathml.Plus+ Symbols.omega + Symbols.InvisibleTimes + Mathml.Literal("E")) + Mathml.PZ) + Mathml.Equal + Mathml.Matrix(vstup.Item1[8], Mathml.TypeBracket.kulate));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Sup(Mathml.LZ + Mathml.Text("D"+ Mathml.Plus + Symbols.omega + Symbols.InvisibleTimes + Mathml.Literal("E")) + Mathml.PZ, Mathml.Row(Mathml.Minus + Mathml.Constant(1))) + Mathml.Equal + Mathml.Matrix(vstup.Item1[9], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.LZ + Mathml.Constant(1) + Mathml.Minus + Mathml.Literal(Symbols.omega) + Mathml.PZ + Mathml.Multiply + Mathml.Literal("D") + Mathml.Equal + Mathml.Matrix(vstup.Item1[10], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Sup(Mathml.LZ + Mathml.Text("D" + Mathml.Plus + Symbols.omega + Symbols.InvisibleTimes + Mathml.Literal("E")) + Mathml.PZ, Mathml.Row(Mathml.Minus + Mathml.Constant(1))) + Mathml.Multiply + Mathml.HLZ +
                Mathml.LZ + Mathml.Constant(1) + Mathml.Minus + Mathml.Literal(Symbols.omega) + Mathml.PZ + Mathml.Multiply + Mathml.Literal("D") +
                Mathml.Minus +
                Mathml.Text(Symbols.omega + Symbols.InvisibleTimes + Mathml.Literal("F")) +
                Mathml.HPZ + Mathml.Equal + Mathml.Matrix(vstup.Item1[11], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.Row(Mathml.Sup(Mathml.LZ + Mathml.Text("D" + Mathml.Plus + Symbols.omega + Symbols.InvisibleTimes + Mathml.Literal("E")) + Mathml.PZ, Mathml.Row(Mathml.Minus + Mathml.Constant(1))) + Mathml.Multiply + Mathml.Text(Symbols.omega + Symbols.InvisibleTimes + Mathml.Literal("b")) + Mathml.Equal + Mathml.Matrix(vstup.Item1[12], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.LineBreak);
            sb.Append(Mathml.EndMath);

            sb.Append(Mathml.HeaderMath(Mathml.Display.inline));
            sb.Append(Mathml.Row(Mathml.Sup(Mathml.Literal("x"), Mathml.Constant(0)) + Mathml.Equal + Mathml.Matrix(vstup.Item2[0], Mathml.TypeBracket.kulate)));
            sb.Append(Mathml.Space() + Mathml.Literal(Symbols.InvisibleTimes) + Mathml.LineBreak);
            sb.Append(Mathml.EndMath);
            sb.Append(Html.Br);
            for (int i = 1; i < vstup.Item2.Count; i++)
            {
                sb.Append(Mathml.HeaderMath(Mathml.Display.inline));
                sb.Append(Mathml.Row(Mathml.Sup(Mathml.Literal("x"), Mathml.Constant(i)) + Mathml.Equal + Mathml.Matrix(vstup.Item1[11], Mathml.TypeBracket.kulate) + Mathml.Multiply + Mathml.Matrix(vstup.Item2[i - 1], Mathml.TypeBracket.kulate) + Mathml.Plus + Mathml.Matrix(vstup.Item1[12], Mathml.TypeBracket.kulate) + Mathml.Equal + Mathml.Matrix(vstup.Item2[i], Mathml.TypeBracket.kulate)));
                sb.Append(Mathml.LineBreak + Mathml.Literal(Symbols.euclidNorm) + Mathml.Sup(Mathml.Literal("x"), Mathml.Constant(i)) + Mathml.Minus + Mathml.Sup(Mathml.Literal("x"), Mathml.Constant(i - 1)) + Mathml.Literal(Symbols.euclidNorm) + Mathml.Equal + (vstup.Item3[i - 1]).NumToMathml());
                sb.Append(Mathml.LineBreak);
                sb.Append(Mathml.EndMath);
                sb.Append(Html.Br);
            }
            return sb.ToString();
        }

        private void ChangeMethod_ListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Linear_solutions_win linear;
            if (ChangeMethod_ListBox.SelectedIndex > -1)
                linear = new Linear_solutions_win((Methods)ChangeMethod_ListBox.SelectedIndex, GetDataOfDataGridView(Soustava_DatagridView), GetDataOfDataGridView(VektorB_dataGridView),
                    (pocatecni_aproximace_dataGridView.Visible) ? GetDataOfDataGridView(pocatecni_aproximace_dataGridView) : null,
                    (faktor_textBox.Visible) ? faktor_textBox.Text : string.Empty,
                    (Pocet_iteraci_textBox.Visible) ? Pocet_iteraci_textBox.Text : string.Empty,
                    (Presnost_textBox.Visible) ? Presnost_textBox.Text : string.Empty);

        }

        private void Linear_solutions_win_Resize(object sender, EventArgs e)
        {
            if ((VektorB_dataGridView.Location.X + VektorB_dataGridView.Width) < splitContainer1.Panel2.Width)
            {
                if ((VektorB_dataGridView.Location.X + VektorB_dataGridView.Width + offset / 2) > ChangeMethod_GroupBox.Location.X + ChangeMethod_GroupBox.Width)
                    splitContainer1.SplitterDistance = VektorB_dataGridView.Location.X + VektorB_dataGridView.Width + offset / 2;
            }
            else
                splitContainer1.SplitterDistance = splitContainer1.Width / 2 + offset/2;     
        }

        private void Pocet_iteraci_textBox_TextChanged(object sender, EventArgs e)
        {
            Pocet_iteraci_textBox.Text = ((TextBox)sender).Text;
            CheckFillTextBoxs();
        }

        private void faktor_textBox_TextChanged(object sender, EventArgs e)
        {
            CheckFillTextBoxs();
        }

        private void VektorB_dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = ((DataGridView)sender);
            if ( dgv[e.ColumnIndex,e.RowIndex].Value != null)
                dgv[e.ColumnIndex, e.RowIndex].Value = Regex.Replace(dgv[e.ColumnIndex,e.RowIndex].Value.ToString(), @"\s+", "");
            CheckFillTextBoxs();
        }

        private void VypocitejButton_Click(object sender, EventArgs e)
        {
            VypocitejButton.Enabled = false;
            DecimalPlaces_numericUpDown.Enabled = false;
            exportyToolStripMenuItem.Enabled = false;
            toolStripStatusLabel1.Text = string.Empty;
            toolStripSplitButton1.Visible = false;
            RemoveHtmlInstance();
            incorrect = false;
            TimeSpan ts = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            pathToHtml = temp + instanceId + method.ToString() + ts.TotalSeconds.ToString() + ".html";

            this.sw = new StreamWriter(pathToHtml);
            if (CheckInput(true))
                sw.Close();
        }

        private void Presnost_textBox_TextChanged(object sender, EventArgs e)
        {
            CheckFillTextBoxs();
        }

        private void AddTask()
        {
            DateTime date = DateTime.Now;
            string file = date.ToString("yyyyMMddHHmmss") + ".xml";
            string[] Files = Directory.GetFiles(Directory.GetCurrentDirectory() + MainMenu.PathTaskLinProperty + MainMenu.dictLinTagName[(int)method]);
            string path = Directory.GetCurrentDirectory() + MainMenu.PathTaskLinProperty + MainMenu.dictLinTagName[(int)method] + "\\" + file;
            StreamWriter stw = new StreamWriter(path);
            stw.WriteLine("<?xml version='1.0' encoding='UTF-8'?>");
            stw.WriteLine("<uloha>");
            if (Pocet_iteraci_textBox.Visible)
                stw.WriteLine("<pocet_iteraci>" + Pocet_iteraci_textBox.Text + "</pocet_iteraci>");
            if (Presnost_textBox.Visible)
                stw.WriteLine("<presnost>" + Presnost_textBox.Text + "</presnost>");
            if (faktor_textBox.Visible)
                stw.WriteLine("<faktor>" + faktor_textBox.Text + "</faktor>");
            stw.WriteLine(DataToXml("Soustava", Soustava_DatagridView));
            stw.WriteLine(DataToXml("VektorB", VektorB_dataGridView));
            if (pocatecni_aproximace_dataGridView.Visible)
                stw.WriteLine(DataToXml("PocatecniAproximace", pocatecni_aproximace_dataGridView));
            stw.WriteLine("</uloha>");
            stw.Close();
            StreamReader sr = new StreamReader(Directory.GetCurrentDirectory() + MainMenu.PathTaskLinProperty + MainMenu.dictLinTagName[(int)method] + "\\" + file);
            string tmp = sr.ReadToEnd().Trim();
            foreach (string item in Files)
            {
                StreamReader str = new StreamReader(item);

                string tmp1 = str.ReadToEnd().Trim();
                if (tmp == tmp1)
                {
                    MessageBox.Show("Tato úloha byla již přidána", "Existující úloha", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    str.Close();
                    sr.Close();
                    File.Delete(Directory.GetCurrentDirectory() + MainMenu.PathTaskLinProperty + MainMenu.dictLinTagName[(int)method] + "\\" + file);
                    return;
                }
                str.Close();
            }
            sr.Close();

            toolStripStatusLabel1.Text = "Úloha byla přidána do cvičných úloh";

        }

        private string DataToXml(string ElementName, DataGridView dgv)
        {
            string[,] tmp = GetDataOfDataGridView(dgv);
            StringBuilder res = new StringBuilder();
            res.Append("<" + ElementName + ">" + Environment.NewLine);
            for (int i = 0; i < tmp.GetLength(0); i++)
            {
                res.Append("<Row>");
                res.Append(Environment.NewLine);
                for (int j = 0; j < tmp.GetLength(1); j++)
                {
                    res.Append("<Cell>");
                    res.Append(tmp[i, j]);
                    res.Append("</Cell>");
                    res.Append(Environment.NewLine);
                }
                res.Append("</Row>");
                res.Append(Environment.NewLine);
            }
            res.Append("</" + ElementName + ">");
            return res.ToString();
        }

        private void AddTask_Button_Click(object sender, EventArgs e)
        {
            if (!CheckInput(false))
                AddTask();
        }

        private void Awesomium_Windows_Forms_WebControl_SizeChanged(object sender, EventArgs e)
        {
            
        }
    }
}
